package com.gxa.eshop.admin.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
public class ActiveMQConfig
{

    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;

    @Value("${spring.activemq.user}")
    private String username;

    @Value("${spring.activemq.password}")
    private String password;

    @Value("${queue-name}")
    private String queueName;



    /**
     * 创建一个Queue对象, 用于在发送或接收的之后的通道对象(还可以用通道名称)
     * @return
     */
    @Bean(name = "queue")
    public Queue queue() {
        return new ActiveMQQueue(queueName);

       // producer.send("goodsCache","1")
        // producer.send(queue,"1");
    }


    @Bean
    public ConnectionFactory connectionFactory(){
        return new ActiveMQConnectionFactory(username, password, brokerUrl);
    }

    /**
     * 创建一个专门处理文字消息的模板对象
     * @return
     */
    @Bean
    public JmsMessagingTemplate jmsMessageTemplate(){
        JmsMessagingTemplate jmsMessagingTemplate =
                new JmsMessagingTemplate(connectionFactory());
        return jmsMessagingTemplate;
    }

    @Bean(name="simpleJmsTemplate")
    public JmsTemplate jmsTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        // 关闭自动事务, 需要手动提交 ,  session对象通过commit()实现事务的提交
        jmsTemplate.setSessionTransacted(false);
        //设置消息的签收方式为4, 表示单条消息的手动签收
        jmsTemplate.setSessionAcknowledgeMode(4);
        return   jmsTemplate ;
    }



}